import requests
from lxml import etree
from multiprocessing.dummy import Pool
import re
url='https://www.pearvideo.com/category_6'
header={'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'}
page_text=requests.get(url=url,headers=header).text
tree=etree.HTML(page_text)
li_list=[]
li_list.extend(tree.xpath('//*[@id="listvideoListUl"]/li/div/a/@href'))
li_list.extend(tree.xpath('//*[@id="categoryList"]/li/div/a/@href'))

def get_video(url):
    url='https://www.pearvideo.com/'+url
    header1 = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'}
    response= requests.get(url=url,headers=header1)
    video_page_text=response.text
    video_page_tree=etree.HTML(video_page_text)
    video_name = video_page_tree.xpath('//*[@id="poster"]/img/@alt')[0]+'.mp4'
    url2='https://www.pearvideo.com/videoStatus.jsp?contId='
    # url2='https://www.pearvideo.com/videoStatus.jsp?contId=1740114&mrd=0.25352294596507075'
    video_id=url.split('_')[-1]
    url2=url2+video_id
    header2 = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
        'Referer': url
    }

    video_url=requests.get(url=url2,headers=header2).json()['videoInfo']['videos']['srcUrl']
    video_url=re.sub('\d{13}','cont-'+video_id,video_url)
    # 'https://video.pearvideo.com/mp4/third/20210827/1630406972689-12081601-202146-hd.mp4'
    # https: // video.pearvideo.com / mp4 / third / 20210827 / cont - 1740193 - 12081601 - 202146 - hd.mp4
    video_content=requests.get(url=video_url,headers=header1).content
    print(video_name,'传输完成！！')
    with open(video_name,'wb') as fp:
        fp.write(video_content)
        print(video_name,'保存完成！！！')


# get_video(li_list[0])
pool=Pool(8)
pool.map(get_video,li_list)

pool.close()
pool.join()

